package pl.edu.wsinf.lukaszwalczak.emailclient.controller;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.inject.Inject;
import org.springframework.stereotype.Component;
import pl.edu.wsinf.lukaszwalczak.emailclient.pojo.Account;
import pl.edu.wsinf.lukaszwalczak.emailclient.pojo.Mail;
import pl.edu.wsinf.lukaszwalczak.emailclient.repository.AccountRepository;
import pl.edu.wsinf.lukaszwalczak.emailclient.service.MailService;
import pl.edu.wsinf.lukaszwalczak.emailclient.view.MailCreateView;

@Component
public class MailCreateController {

    @Inject
    private MailCreateView view;
    @Inject
    private MailService mailService;
    @Inject
    private AccountRepository accountRepository;

    public Mail save(Account account, Map<String, Object> values) {
        return mailService.save(account, values);
    }

    public void send(Mail mail) {
        mailService.send(mail);
    }

    public void showWindow(Mail mail) {
        view.show(mail);
    }
    
    public List<Account> getAccounts() {
        return accountRepository.findAll();
    }

    public void setReplyValues(Mail mail, boolean replyAll) {
        Map<String, Object> values = new HashMap<>();

        Set<String> to = new HashSet<>();

        if (mail.getFrom() != null && mail.getFrom().getAddress() != null) {
            to.add(mail.getFrom().getAddress());
        }

        if (replyAll) {
            if (mail.getTo() != null) {
                to.addAll(mail.getTo());
            }
            if (mail.getAccount() != null && mail.getAccount().getEmailAddress() != null) {
                if (to.contains(mail.getAccount().getEmailAddress())) {
                    to.remove(mail.getAccount().getEmailAddress());
                }
            }
            Set<String> cc = new HashSet<>();
            if (mail.getCc() != null) {
                cc.addAll(mail.getCc());
            }
            values.put("cc", cc);
        }

        String content = "<br/><br/><hr/><div>";
        if (mail.getSentDate() != null) {
            content += "Dnia " + mail.getSentDate().toString("dd/MM/yyyy HH:mm") + " ";
        }
        if (mail.getFrom() != null && mail.getFrom().getAddress() != null) {
            if (mail.getFrom().getPersonal() != null && !mail.getFrom().getPersonal().isEmpty()) {
                content += mail.getFrom().getPersonal();
            } else if (mail.getFrom().getAddress() != null && !mail.getFrom().getAddress().isEmpty()) {
                content += mail.getFrom().getAddress();
            }
            content += " napisał:";
        }
        content += "</div>";
        if (mail.getContent() != null) {
            content += "<blockquote>";
            if (mail.getContent().getHtml() != null && !mail.getContent().getHtml().isEmpty()) {
                content += mail.getContent().getHtml();
            } else if (mail.getContent().getPlain() != null && !mail.getContent().getPlain().isEmpty()) {
                content += mail.getContent().getPlain();
            }
            content += "</blockquote>";
        }

        values.put("to", to);
        values.put("subject", "RE: " + mail.getSubject());
        values.put("content", content);

        view.setValues(values);
    }

    public void setForwardValues(Mail mail) {
        Map<String, Object> values = new HashMap<>();

        String content = "<br/><br/><hr/><div>";
        if (mail.getSentDate() != null) {
            content += "Dnia " + mail.getSentDate().toString("dd/MM/yyyy HH:mm") + " ";
        }
        if (mail.getFrom() != null && mail.getFrom().getAddress() != null) {
            if (mail.getFrom().getPersonal() != null && !mail.getFrom().getPersonal().isEmpty()) {
                content += mail.getFrom().getPersonal();
            } else if (mail.getFrom().getAddress() != null && !mail.getFrom().getAddress().isEmpty()) {
                content += mail.getFrom().getAddress();
            }
            content += " napisał:";
        }
        content += "</div>";
        if (mail.getContent() != null) {
            content += "<blockquote>";
            if (mail.getContent().getHtml() != null && !mail.getContent().getHtml().isEmpty()) {
                content += mail.getContent().getHtml();
            } else if (mail.getContent().getPlain() != null && !mail.getContent().getPlain().isEmpty()) {
                content += mail.getContent().getPlain();
            }
            content += "</blockquote>";
        }
        values.put("subject", "FWD: " + mail.getSubject());
        values.put("content", content);

        view.setValues(values);
    }
}
